package com.tourism.controller;

import com.github.pagehelper.PageInfo;
import com.tourism.entity.User;
import com.tourism.entity.UserQuery;
import com.tourism.exception.GlobalException;
import com.tourism.service.UserService;
import com.tourism.util.common.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 用户相关的控制器
 * @Author z
 * @Date 2021-03-01 20:40
 */
@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 根据用户id查询用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/geUserById")
    @ResponseBody
    public User getUserById(Long id) {
        return userService.getUserById(id);
    }


    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    @ResponseBody
    public Response addUser(@RequestBody User user) throws GlobalException {

        return Response.success(userService.addUser(user));

    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    @ResponseBody
    public Response updateUser(@RequestBody User user) throws GlobalException {

        return Response.success(userService.updateUser(user));

    }


    /**
     * 修改用户冻结状态
     *
     * @param id
     * @param freeze
     * @return
     */
    @PostMapping("/updateUserFreeze")
    @ResponseBody
    public Response updateUserFreeze(Long id, Boolean freeze) throws GlobalException {

        return Response.success(userService.updateUserFreeze(id, freeze));

    }

    /**
     * 修改用户密码
     *
     * @param id
     * @param password
     * @return
     */
    @PostMapping("/updateUserPassword")
    @ResponseBody
    public Response updateUserPassword(Long id, String password, String newPassword) throws GlobalException {

        return Response.success(userService.updateUserPassword(id, password, newPassword));

    }

    /**
     * 新增用户
     *
     * @param username
     * @param id
     * @return
     */
    @PostMapping("/existUser")
    @ResponseBody
    public boolean existUser(String username, Long id) {
        return userService.existUser(username, id);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping ("/deleteUserById")
    @ResponseBody
    public Response deleteUserById(Long id) {//Response作用
        return Response.success(userService.deleteUserById(id));
    }


    /**
     * 查询用户列表
     *
     * @param userQuery
     * @return
     */
    @PostMapping("/getUserList")
    @ResponseBody
    public PageInfo<User> getUserList(@RequestBody UserQuery userQuery) throws GlobalException {
        return userService.getUserList(userQuery);
    }

}
